LDFLAGS ?= $(shell ./hack/version.sh)

GOVER_MAJOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\1/")
GOVER_MINOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\2/")
GO113 := $(shell [ $(GOVER_MAJOR) -gt 1 ] || [ $(GOVER_MAJOR) -eq 1 ] && [ $(GOVER_MINOR) -ge 13 ]; echo $$?)
ifeq ($(GO113), 1)
$(error Please upgrade your Go compiler to 1.13 or higher version)
endif

# Enable GO111MODULE=on explicitly, disable it with GO111MODULE=off when necessary.
export GO111MODULE := on
GOOS ?= linux
GOARCH ?= $(shell go env GOARCH)
GOENV  := GOOS=$(GOOS) GOARCH=$(GOARCH)
GO     := $(GOENV) go
ifeq ("${ENABLE_FIPS}", "1")
GO_BUILD := GOEXPERIMENT=boringcrypto CGO_ENABLED=1 $(GO) build -trimpath -tags boringcrypto
else
GO_BUILD := CGO_ENABLED=0 $(GO) build -trimpath
endif

DOCKER_REGISTRY ?= dimvex.com
DOCKER_REPO ?= ${DOCKER_REGISTRY}/phant
IMAGE_TAG ?= latest
TEST_COVER_PACKAGES := go list ./cmd/... ./pkg/... $(foreach mod, $(GO_SUBMODULES), $(mod)/...) | grep -vE "pkg/client" | grep -vE "pkg/apis/pingcap" | sed 's|gauss-operator/|./|' | tr '\n' ','

# NOTE: coverage report generated for E2E tests (with `-c`) may not stable, see
# https://github.com/golang/go/issues/23883#issuecomment-381766556
GO_TEST := $(GO) test -cover -covermode=atomic -coverpkg=$$($(TEST_COVER_PACKAGES))

default: build

docker-push: docker
	docker push "${DOCKER_REPO}/phant-operator:${IMAGE_TAG}"

docker: operator-docker

ifeq ($(NO_BUILD),y)
operator-docker:
	@echo "NO_BUILD=y, skip build for $@"
else
operator-docker: build
endif
ifeq ($(E2E),y)
	docker build --tag "${DOCKER_REPO}/phant-operator:${IMAGE_TAG}" -f images/phant-operator/Dockerfile.e2e images/phant-operator
else
	docker build --tag "${DOCKER_REPO}/phant-operator:${IMAGE_TAG}" --build-arg=TARGETARCH=$(GOARCH) images/phant-operator
endif

build: controller-manager 

controller-manager:
ifeq ($(E2E),y)
	$(GO_TEST) -ldflags '$(LDFLAGS)' -c -o images/phant-operator/bin/phant-controller-manager ./cmd/controller-manager
else
	$(GO_BUILD) -ldflags '$(LDFLAGS)' -o images/phant-operator/bin/$(GOARCH)/phant-controller-manager cmd/controller-manager/main.go
endif


gocovmerge:
	GOBIN=$(shell pwd)/bin/ $(GO) install github.com/zhouqiang-cl/gocovmerge@latest

fault-trigger:
	$(GO_BUILD) -ldflags '$(LDFLAGS)' -o tests/images/fault-trigger/bin/fault-trigger tests/cmd/fault-trigger/*.go

# ARGS:
#
# GOFLAGS: Extra flags to pass to 'go' when building, e.g.
# 		`-v` for verbose logging.
# 		`-race` for race detector.
# GO_COVER: Whether to run tests with code coverage. Set to 'y' to enable coverage collection.
#
test: TEST_PACKAGES = ./pkg 
test: ## Run unit tests
	@echo "Run unit tests"
ifeq ($(GO_COVER),y)
	go test -cover \
		$(foreach pkg, $(TEST_PACKAGES), $(pkg)/...) \
		$(foreach mod, $(GO_SUBMODULES), $(mod)/...) \
		-coverpkg=$$($(TEST_COVER_PACKAGES)) -coverprofile=coverage.txt -covermode=atomic
else
	go test \
		$(foreach pkg, $(TEST_PACKAGES), $(pkg)/...) \
		$(foreach mod, $(GO_SUBMODULES), $(mod)/...)
endif
	@echo -e "\nUnit tests run successfully!"

ALL_CHECKS = EOF codegen crd boilerplate openapi-spec api-references spelling modules lint
check: $(addprefix check-,$(ALL_CHECKS)) tidy

check-%:
	./hack/verify-$*.sh

generate:
	./hack/update-all.sh

crd:
	./hack/update-crd.sh

api-references:
	./hack/update-api-references.sh

tidy:
	@echo "go mod tidy"
	go mod tidy && git diff -U --exit-code go.mod go.sum
	cd pkg/apis && go mod tidy && git diff -U --exit-code go.mod go.sum
	cd pkg/client && go mod tidy && git diff -U --exit-code go.mod go.sum

.PHONY: check  build e2e-build debug-build cli e2e gocovmerge test docker e2e-docker debug-build-docker
